package com.kuang.annotation;

import java.lang.annotation.*;
import java.lang.reflect.Field;

public class tset13 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class user = Class.forName("com.kuang.annotation.User1");
        Annotation[] annotations = user.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
        //注解的类的class
        UserType userType = (UserType) user.getAnnotation(UserType.class);
        String value = userType.value();
        System.out.println(value);

        Field name = user.getDeclaredField("name");
        UserField annotation = (UserField) name.getAnnotation(UserField.class);
        System.out.println(annotation.username()+"////"+annotation.type()+"////"+annotation.length());

    }
}

@UserType("db_user")
class User1{
    @UserField(username = "username",type = "varchar",length = 7)
    private String name;
    @UserField(username = "userid",type = "int",length = 11)
    private int id;
    @UserField(username = "userage",type = "int",length = 3)
    private int age;

    public User1() {
    }

    public User1(String name, int id, int age) {
        this.name = name;
        this.id = id;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User1{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", age=" + age +
                '}';
    }
}

//类的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface UserType{
    String value();
}

//属性的注解
//作用域
@Target(ElementType.FIELD)
//注解什么时候发生
@Retention(RetentionPolicy.RUNTIME)
@interface UserField{
    String username();
    String type();
    int length();
}